<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
?>
<!-- 主体内容 -->
<div id="excel-wrapper" class="container">
    <div class="row">
        <div class="col-md-9">
            <div class="panel panel-primary">
                <div id="function-module-name" class="panel-heading">添加排期</div>
                <div class="panel-body">
                    <div id="addschedule-container">

                    </div>
                    <div id="excel-container">

                    </div>
                </div>
            </div>
            <button id="btn-save" class="btn btn-danger" data-toggle="modal" data-target="#save-confirm">保存</button>
            <!--          <button id="dump">dump console</button>-->
        </div>
        <div class="col-md-3">
            <div class="panel panel-primary">
                <div  class="panel-heading">管理列表</div>
                <div class="panel-body">
                    <!--      列表信息--> 
                    <ul id="sheet-list" class="nav nav-pills nav-stacked">
                        <li id="btn-add-schedule"><a href="#">添加排期</a></li>
                        <li id="btn-alter-schedule" ><a href="#">修改排期</a></li>
                        <li id="btn-emp-management" ><a href="#">人员管理</a></li>

                    </ul>
                    <!-- <button id="btn-create-sheet" class="btn btn-default btn-sm">添加排期</button>
                    -->

                </div>
            </div>
        </div>
    </div>

</div>
<script type="text/javascript" charset="utf-8">




//响应标题点击事件



//    $(document).on('click', '#nav-list li', function(e) {
//        var $target = $(e.target);
//        $('#nav-list li').each(function() {
//            $(this).removeClass('active');
//        });
//        $target.parent().addClass('active');
//    });
// $(function() {
//     buildaddschedule();//创建添加排期的页面
////        buildExcel();  //绘制Excel
//     });

    //初始化

    function buildaddschedule() {
        //$('#addschedule-container').append(' <h3> <span class="label label-default">项目：</span></h3>');
        $('#addschedule-container').append(' <div class="input-group"><span class="input-group-addon">项目</span><input id="project" type="text" class="form-control" placeholder="不超过8个字哦"></div>');
        $('#addschedule-container').append(' <div class="input-group"><span class="input-group-addon">模块</span><input id="module" type="text" class="form-control" placeholder="不超过8个字哦"></div>');
        $('#addschedule-container').append(' <div class="input-group"><span class="input-group-addon">功能</span><input id="function" type="text" class="form-control" placeholder="不超过8个字哦"></div>');
        $('#addschedule-container').append(' <div class="input-group"><span class="input-group-addon">详情</span><input id="detail" type="text" class="form-control" placeholder="随便输入"></div>');


        //面板

        //      $('#addschedule-container').append(' <div class="panel panel-default"><div class="panel-heading">详情</div><div class="panel-body"><p>...</p></div> <table class="table"><th><td>123</td><td>123</td><td>123</td></th></table>');
        //  $('#addschedule-container').append(' <div class="panel panel-default"><div class="panel-heading">详情</div></div> ');

        ///  $('#addschedule-container').append(' <div class="btn-toolbar" data-role="editor-toolbar" data-target="#editor"><a class="btn btn-large" data-edit="bold"><i class="icon-bold"></i></a></div><div id="editor" style="overflow:scroll; max-height:300px;height:200px"}></div>');
        //     $('#editor').wysiwyg();
    }
//$(document).ready(function() {
//        alert('123');
//    });
    $(function() {
     //   alert('123');
        buildaddschedule();//创建添加排期的页面
        //   buildExcel();  //绘制Excel
//        var sheetContent  //数据内容
//                , $container = $("#excel-container")
//                , handsontable = $container.data('handsontable')
//                , blankContent = handsontable.getData()
//                , currentSheetTitle = ""
//                , currentSheetContent = blankContent
//                ;
//        $.ajax({
//            type: "post",
//            url: "src/controlers/load_controler.php",
//            data: "",
//            success: function(result) {
//                var results = JSON.parse(result);
//
//                // alert(results);
//
//                var sLatestView = localStorage.getItem('latestView')
//                        , sSheetTitle
//                        , $sheetList = $('#sheet-list')
//                        , lengthes = results.length;
//
//                //alert(lengthes);
//
//                for (var i = 0; i < lengthes; i++) {
//                    sSheetTitle = results[i].toString();
//                    if (sSheetTitle == sLatestView) {
//                        $sheetList.append('<li class="active"><a href="###">' + sSheetTitle + '</a></li>');
//                        //    alert(sSheetTitle);
//                        loadContent(sSheetTitle);  //加载缓存标题的最新数据
//
//                        currentSheetTitle = sSheetTitle;  //设置当前标题
//                    } else {
//                        $sheetList.append('<li><a href="###">' + sSheetTitle + '</a></li>');
//                    }
//                }
//            },
//            error: function() {
//                alert("不能获得值");
//            }
//        }
//        );
        // });


        //响应点击列表项内容

        $(document).on('click', '#sheet-list li', function(e) {  //查
//                    console.log(handsontable.getData());
//                    console.log(currentSheetContent);
            // if (handsontable.getData() == currentSheetContent) {
            var $target = $(e.target);
            $('#sheet-list li').each(function() {
                $(this).removeClass('active');
            });
            $target.parent().addClass('active');

            $("#function-module-name").html($target.html());
//                currentSheetTitle = $target.html();
//                loadContent(currentSheetTitle);
//                currentSheetContent = handsontable.getData();
//            } else {
//                $('.alert').show();
//            }
        });

        $("#btn-save").on('click', function() {  //增、改
            // currentSheetContent = handsontable.getData();
            project = $("#project").val();
            

            alert(project);
        });
        //保存，修改内容，弹窗后点击保存按钮，触发事件

        $(document).on('click', 'button[name=save]', function() {  //增、改
            // currentSheetContent = handsontable.getData();
            project = $("#project").val();

            alert(project);

            // $('#save-confirm').modal('hide');
            if (!currentSheetTitle) {
                //        console.log(currentSheetTitle);
                currentSheetTitle = $('#sheet-title').val();
                localStorage.setItem('latestView', currentSheetTitle);
                $.ajax({
                    type: "post",
                    url: "src/controlers/save_controler.php",
                    data: {"sheetTitle": currentSheetTitle,
                        "sheetContent": currentSheetContent},
                    success: function(msg) {
                        alert("保存成功！ ");
                        //  alert("保存成功！ " + msg.toString());
                    },
                    error: function() {
                        alert("保存失败！");
                    }
                }
                );
                $('#sheet-list li:last-child').remove();
                $('#sheet-list').append('<li class="active"><a href="###">' + currentSheetTitle + '</a></li>');
            } else {
                $.ajax({
                    type: "post",
                    url: "src/controlers/update_controler.php",
                    data: {"sheetTitle": currentSheetTitle,
                        "sheetContent": currentSheetContent},
                    success: function(msg) {
                        alert("修改成功");
                    },
                    error: function() {
                        alert("修改失败");
                    }
                }
                );
            }
        });


        //新建列表功能，事件触发器


        $('#btn-create-sheet').on('click', function() {  //增入口
            buildExcel();
            $('#sheet-list li').each(function() {
                $(this).removeClass('active');
            });
            $('#sheet-list').append('<li class="active"><a href="###">未命名文档</a></li>');
            currentSheetTitle = "";
            currentSheetContent = "";
        });


        //测试时打印输出台功能
        $('#dump').click(function() {
            console.log(handsontable.getData());
            console.log(currentSheetContent);
        });

 
    //加载表格内容
    function loadContent(sheetTitle) {
        $.ajax({
            type: "post",
            url: "src/controlers/select_controler.php",
            data: {"sheetTitle": sheetTitle},
//                         
            success: function(str) {


                //     alert(str);


                var object = JSON.parse(str);
                handsontable.loadData(object);
                localStorage.setItem('latestView', sheetTitle);
                currentSheetTitle = sheetTitle;
                currentSheetContent = object;
            },
            error: function() {
                alert("不能获得值");
            }
        }
        );
        }
    }
    );
            function buildExcel() {
                var data = [
                    {isOK: "", project: "", module: "", functions: "", person: "", deadline: ""}
                ];
                var $tmpExcel = $('#excel-container');
                $tmpExcel.handsontable({
                    data: data,
                    minSpareRows: 1,
                    colHeaders: ["是否完成", "项目", "项目模块", "功能", "人员", "完成日期"],
                    //      contextMenu: true,
                    columnSorting: true,
                    columns: [
                        {
                            data: "isOK",
                            type: "checkbox",
                            checkedTemplate: "true",
                            uncheckedTemplate: ""
                        },
                        {
                            data: "project"
                        },
                        {
                            data: "module"
                        },
                        {
                            data: "functions"
                        },
                        {
                            data: "person",
                            type: 'autocomplete',
                            source: ["毛鹏宇", "刘鑫", "赵峰", "马胜盼", "翟飞宇", "于涛", "寇德键", "刘畅", "李志辉", "孙浩", "王晓远"],
                            strict: false
                        },
                        {
                            data: "deadline",
                            type: 'date',
                            dateFormat: 'yy-mm-dd'
                        }
                    ],
                    cells: function(row, col, prop) {
                        var cellProperties = {};
                        if ($tmpExcel.handsontable('getData')[row][prop] !== '') {
                            cellProperties.readOnly = true;
                        }
                        return cellProperties;
                    }


                });
            }
</script>
